package com.sctvcloud.qingchuan.ui.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.ruihang.generalibrary.ui.dialog.BaseBottomSheetFrag;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.qingchuan.R;

/**
 * 编辑吐槽内容
 */
public class AccountEditWarningFragment extends BaseBottomSheetFrag {


    private CustomFontTextView to_back;
    private CustomFontTextView to_send;
    private EditText edit_warning;
    private CustomFontTextView surplus;

    private OnWarningSendListener sendListener;
    private String beforeStr;


    public AccountEditWarningFragment() {
    }


    public void setSendListener(OnWarningSendListener sendListener) {
        this.sendListener = sendListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public int getLayoutResId() {
        return R.layout.dia_fragment_warning_edit;
    }

    public void initView() {
        to_back = (CustomFontTextView) rootView.findViewById(R.id.to_back);
        to_send = (CustomFontTextView) rootView.findViewById(R.id.to_send);
        surplus = (CustomFontTextView) rootView.findViewById(R.id.surplus);
        surplus.setText("0/100");
        edit_warning = (EditText) rootView.findViewById(R.id.edit_warning);
        to_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccountEditWarningFragment.this.dismissAllowingStateLoss();
                String text = edit_warning.getText().toString().trim();
                if (TextUtils.isEmpty(text)) {
                    toast("请编辑吐槽内容");
                } else {
                    sendListener.OnWarningSend(text);
                }

            }
        });
        to_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AccountEditWarningFragment.this.dismissAllowingStateLoss();
            }
        });

        edit_warning.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                beforeStr = s.toString();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = edit_warning.getText().toString().trim();
                if (str.length() > 100) {
                    edit_warning.setText(beforeStr);
                    toast("最多输入100字符");
                }
                String afStr = edit_warning.getText().toString().trim();
                surplus.setText(afStr.length() + "/100");
                edit_warning.setSelection(afStr.length());//将光标移至文字末尾
            }
        });

    }

    public interface OnWarningSendListener {
        void OnWarningSend(String editWarning);
    }
}
